<div class="layuimini-main">

    <div class="layui-form layuimini-form">
        <div class="layui-form-item">
            <label class="layui-form-label required">登录账号</label>
            <div class="layui-input-block">
                <input type="text" id="username" name="username" lay-verify="required" lay-reqtext="登录账号不能为空" placeholder="请输入登录账号" value="" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label required">用户名</label>
            <div class="layui-input-block">
                <input type="text" id="nickName" name="nickName" lay-verify="required" lay-reqtext="用户名不能为空" placeholder="请输入用户名" value="" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label required">登录密码</label>
            <div class="layui-input-block">
                <input type="password" id="password" name="password" lay-verify="required" lay-reqtext="登录密码不能为空" placeholder="请输入登录密码" value="" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button>
            </div>
        </div>
    </div>
</div>
<script>
    layui.use(['form', 'table'], function () {
        var form = layui.form,
            layer = layui.layer,
            table = layui.table,
            $ = layui.$;
        var childEditData = parent.editData;
        /**
         * 初始化表单，要加上，不然刷新部分组件可能会不加载
         */
        form.render();
        if(childEditData){
            $('#username').val(childEditData.username);
            $('#nickName').val(childEditData.nickName);
            $('#password').val(childEditData.password);
        }
        // 当前弹出层，防止ID被覆盖
        var parentIndex = layer.index;
        //监听提交
        form.on('submit(saveBtn)', function (data) {
            var submitData = data.field;
            var method = "POST";
            if(childEditData){
                method = "PUT";
                submitData.id = childEditData.id;
            }
            $.ajax({
                url:"user",
                type:method,
                data:submitData,
                success:function (result) {
                    if(result.code==200){
                        layer.msg('操作成功', {icon: 1});
                        layer.close(parentIndex);
                        window.parent.layui.table.reload('currentTableId');
                    }else{
                        layer.msg("操作失败，失败原因："+result.message, {icon: 2});
                    }
                }
            });
        });

    });
</script>